<template>
  <div>
    <div style="padding:20px 10px; border-bottom:1px solid #ebebeb">
      <span>主办校区列表</span>
    </div>
    <el-col class="add-btn" :span="4">
      <el-button type="primary" @click=";centerDialogVisible1 = true">新增</el-button>
    </el-col>

    <template>
      <el-table border :data="tableData" style="width: 100%">
        <el-table-column type="index" :index="indexMethod" align="center"></el-table-column>
        <el-table-column label="所属赛区" prop="area.area_name" align="center"></el-table-column>
        <el-table-column label="校区名称" prop="school_name" align="center"></el-table-column>
        <el-table-column label="校区地址" prop="school_url" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleClick(scope.row);centerDialogVisible = true">编辑</el-button>
            <el-button size="mini" type="danger" @click="prohibit(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <!-- 编辑弹框 -->
    <el-dialog title="编辑信息" :visible.sync="centerDialogVisible" width="30%" center>
      <!-- 编辑弹框 -->
      <div class="form-list">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="所属赛区">
            <el-select v-model="form.area" placeholder="赛区选择" style="display:block">
              <el-option
                v-for="item in areaData"
                :key="item.id"
                :label="item.area_name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="校区名称">
            <el-input v-model="form.school_name"></el-input>
          </el-form-item>

          <el-form-item label="校区地址">
            <el-input v-model="form.school_url" disabled></el-input>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit(form); centerDialogVisible = false">提交</el-button>
      </span>
    </el-dialog>

    <!-- 新增弹框 -->
    <el-dialog title="编辑信息" :visible.sync="centerDialogVisible1" width="30%" center>
      <!-- 新增表格 -->
      <div class="form-list">
        <el-form ref="form1" status-icon :model="form1" label-width="80px">
          <el-form-item label="所属赛区">
            <el-select v-model="form1.area_id" placeholder="赛区选择" style="display:block">
              <el-option
                v-for="item in areaData"
                :key="item.id"
                :label="item.area_name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="校区名称">
            <el-input v-model="form1.school_name"></el-input>
          </el-form-item>
          <el-form-item label="校区地址">
            <el-input v-model="form1.school_url"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="addsubtn(); centerDialogVisible1 = false">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>



<script>
import { log } from "util";
export default {
  name: "school",
  data() {
    return {
      // 新增弹框默认状态
      centerDialogVisible: false,
      centerDialogVisible1: false,

      tableData: [],
      areaData: [],
      current_area: parseInt(this.$store.state.area_id),
      // 编辑列表信息
      form: {
        school_id: "",
        school_name: "",
        school_url: ""
      },
      // 新增列表信息
      form1: {
        area_id: "",
        school_name: "",
        school_url: ""
      }
    };
  },

  mounted: function() {
    this.schoolListInfo();
    this.areaListInfo();
  },

  methods: {
    // 获取举办赛区信息
    areaListInfo() {
      let that = this;
      if (that.$store.state.is_superuser) {
        that.$api
          .getAreaInfo()
          .then(res => {
            that.areaData = res.data;
          })
          .catch(err => {});
      } else {
        let id = that.$store.state.area_id;
        let area_name = that.$store.state.AreaName;
        that.areaData = [{ id, area_name }];
        // console.log(that.areaData)
      }
    },

    // 获取举办校区信息
    schoolListInfo() {
      let that = this;
      that.$api
        .getSchoolListInfo()
        .then(res => {
          that.tableData = res.data;
        })
        .catch(err => {});
    },

    // ===========================================================

    // 编辑校区--获取行数据
    handleClick(row) {
      let that = this;
      that.form.school_id = row.id;
      that.form.school_name = row.school_name;
      that.form.school_url = row.school_url;
    },

    // 编辑校区-数据提交

    onSubmit(form) {
      let that = this;
      let postData = this.$qs.stringify({
        school_name: form.school_name
      });
      that.$api
        .schoolListItemInfo(form.school_id, postData)
        .then(res => {
          that.tableData.forEach(item => {
            if (item.id === form.school_id) {
              item.school_name = res.data.school_name;
            }
          });
          that.$message({
            type: "success",
            message: "修改成功!"
          });
        })
        .catch(err => {
          that.$message({
            type: "error",
            message: "修改失败!"
          });
        });
    },

    // ===========================================================

    //  新增校区
    addsubtn() {
      let that = this;
      console.log(that.form1);
      let PostData = this.$qs.stringify({
        area: that.form1.area_id,
        school_name: that.form1.school_name,
        school_url: that.form1.school_url
      });
      that.$api
        .addSchoolInfo(PostData)
        .then(res => {
          // console.log(res);
          if (res.status == 201) {
            // console.log(res.data);
            that.schoolListInfo();
            that.$message({
              type: "success",
              message: "新增成功!"
            });
          }
        })
        .catch(err => {
          console.log(err);
        });
    },

    // ============================================================

    //   删除按钮
    prohibit(obj) {
      console.log(obj);
      let that = this;
      that.$api
        .DeletSchoolInfo(obj.id, { is_active: 0 })
        .then(res => {
          // console.log(res)
          if (res.status == 200) {
            that.schoolListInfo();
            that.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        })
        .catch(err => {});
    },

    // 索引排序
    indexMethod(index) {
      return index * 1;
    }
  }
};
</script>

<style scoped>
.add-btn {
  margin: 30px 30px 30px 10px;
  padding-right: 120px;
}
</style>


